#!/usr/bin/python
#-*- coding: UTF-8 -*-

import httplib,random
import Image
from crackcode.crackcode import image2code
fake_ip = '10.3.' + str(random.randint(1,254)) +"."+ str(random.randint(1,254))


headers  = {
            "Host":"www.itab.cc"
            ,"Connection":"keep-alive"
            ,"User-Agent":"Mozilla/5.0 (Windows NT 6.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"
            ,"Referer":"http://www.itab.cc"
            ,"Content-Type":"text/html; charset=UTF-8"
            ,"Accept-Charset": "UTF-8,*;q=0.5"
 #           ,"X-Forwarded-For":fake_ip
            }

conn = httplib.HTTPConnection('www.itab.cc')
conn.request('GET','/code.php','',headers)
r1 = conn.getresponse()
im = r1.read()
r1.close()
conn.close()
cookie = r1.getheader('set-cookie').split()[0][:-1]
a = file('output.png','w') 
a.write(im)
a.close()

headers['Cookie']=cookie
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['Referer'] = 'http://www.itab.cc/toupiao.php'
conn = httplib.HTTPConnection('www.itab.cc')

post_data = ''
for i in [1, 2, 12, 24, 29]:
    post_data += ('r%5B%5D=' + str(i) + '&')
post_data +=('code=' + image2code('output.png'))
conn.request('POST','/toupiao_ok.php',post_data ,headers)
r1 = conn.getresponse()
res_text = r1.read()
r1.close()
conn.close()

print res_text.decode('gbk').encode('utf-8') 

